package com.ruoyi.net.service.impl;

import java.util.Collection;
import java.util.List;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.net.mapper.SysPerformanceInfoMapper;
import com.ruoyi.net.domain.SysPerformanceInfo;
import com.ruoyi.net.service.ISysPerformanceInfoService;

import javax.annotation.PostConstruct;

/**
 * 性能字典信息Service业务层处理
 * 
 * @author tony
 * @date 2025-02-20
 */
@Service
public class SysPerformanceInfoServiceImpl implements ISysPerformanceInfoService 
{
    @Autowired
    private SysPerformanceInfoMapper sysPerformanceInfoMapper;

    @Autowired
    private RedisCache redisCache;

    @PostConstruct
    public void init() {
        loadPerformanceInfo();
    }

    @Override
    public void loadPerformanceInfo() {
        List<SysPerformanceInfo> list = sysPerformanceInfoMapper.selectSysPerformanceInfoList(new SysPerformanceInfo());
        for(SysPerformanceInfo info : list) {
            redisCache.setCacheObject(getCacheKey(info.getPmCode().toString()), info);
        }
    }

    @Override
    public void resetConfigCache() {
        clearPerformanceInfoCache();
        loadPerformanceInfo();
    }

    /**
     * 清空缓存数据
     */
    @Override
    public void clearPerformanceInfoCache() {
        Collection<String> keys = redisCache.keys(CacheConstants.PERFORMANCE_INFO_KEY + "*");
        redisCache.deleteObject(keys);
    }

    /**
     * 查询性能字典信息
     * 
     * @param id 性能字典信息主键
     * @return 性能字典信息
     */
    @Override
    public SysPerformanceInfo selectSysPerformanceInfoById(String id)
    {
        return sysPerformanceInfoMapper.selectSysPerformanceInfoById(id);
    }

    /**
     * 查询性能字典信息列表
     * 
     * @param sysPerformanceInfo 性能字典信息
     * @return 性能字典信息
     */
    @Override
    public List<SysPerformanceInfo> selectSysPerformanceInfoList(SysPerformanceInfo sysPerformanceInfo)
    {
        return sysPerformanceInfoMapper.selectSysPerformanceInfoList(sysPerformanceInfo);
    }

    /**
     * 新增性能字典信息
     * 
     * @param sysPerformanceInfo 性能字典信息
     * @return 结果
     */
    @Override
    public int insertSysPerformanceInfo(SysPerformanceInfo sysPerformanceInfo)
    {
        String uuid = IdUtils.simpleUUID();
        if (StringUtils.isEmpty(sysPerformanceInfo.getId())) {
            sysPerformanceInfo.setId(uuid);
        }
        return sysPerformanceInfoMapper.insertSysPerformanceInfo(sysPerformanceInfo);
    }

    /**
     * 修改性能字典信息
     * 
     * @param sysPerformanceInfo 性能字典信息
     * @return 结果
     */
    @Override
    public int updateSysPerformanceInfo(SysPerformanceInfo sysPerformanceInfo)
    {
        return sysPerformanceInfoMapper.updateSysPerformanceInfo(sysPerformanceInfo);
    }

    /**
     * 批量删除性能字典信息
     * 
     * @param ids 需要删除的性能字典信息主键
     * @return 结果
     */
    @Override
    public int deleteSysPerformanceInfoByIds(String[] ids)
    {
        return sysPerformanceInfoMapper.deleteSysPerformanceInfoByIds(ids);
    }

    /**
     * 删除性能字典信息信息
     * 
     * @param id 性能字典信息主键
     * @return 结果
     */
    @Override
    public int deleteSysPerformanceInfoById(String id)
    {
        return sysPerformanceInfoMapper.deleteSysPerformanceInfoById(id);
    }


    private String getCacheKey(String key)
    {
        return CacheConstants.PERFORMANCE_INFO_KEY + key;
    }
}
